import { API_BASE_URL_APPLICATION } from '@/static/js/requestBase.js';
import {
	des_decrypt
} from '@/static/js/crypto.js';
const request = (url, method, data = {}, showLoading = false, base_name) => {
	base_name = base_name || API_BASE_URL_APPLICATION;
	return new Promise((resolve, reject) => {
		// #ifdef APP-PLUS
		let pages = getCurrentPages(); // 获取当前页面栈的实例
		let currPage = pages[pages.length - 1]; //当前页面
		// #endif
		const token = getApp().globalData.token;
		const provinceInfo = getApp().globalData.provinceLiscationInfo || {};
		const provinceLiscationInfo = uni.getStorageSync('provinceLiscationInfo') || {};


		if (showLoading) {
			uni.showLoading({
				title: "",
				mask: true
			})
		}
		let api = base_name + url
		let tempData = Object.assign({}, {
			province_id: provinceInfo.province_id || provinceLiscationInfo.province_id || 1,
			invite_id: getApp().globalData.invite_id
		}, data);
		uni.request({
			url: api,
			data: tempData,
			method: method,
			header: { //自定义请求头
				'Content-type': 'application/json',
				'x-mp-word-token': token,
				// #ifdef APP-PLUS
				'referer': currPage ? currPage.route : ''
				// #endif
			},
			success: res => {
				if (showLoading) {
					uni.hideLoading();
				}
				let code = res.data.code;
				// if (res.data.is_enc && res.data.data) {
				// 	// 解密数据
				// 	res.data.data = JSON.parse(des_decrypt(res.data.data, token, selfInfo.id))
				// }
				if (res.data.is_enc && res.data.data) {
					let selfCookie = res.header['x-gov-token'];
					let num = 0;
					for (let index = 0; index < selfCookie.length; index++) {
						num += parseInt(selfCookie.charCodeAt(index).toString(10));
					}
					res.data.data = JSON.parse(des_decrypt(res.data.data, selfCookie, num))
				}
				
				if (code === 0) {
					resolve(res.data);
				} else if (code == 2) {
					reject(res.data);
					uni.removeStorageSync('token')
				} else if (code == 401) {
					if (selfInfo.id) {
						uni.setStorageSync('selfInfo', {
							id: 0
						});
					}
					if (token) {
						getApp().globalData.token = '';
					}
					resolve(res.data);
				} else {
					let info = res.data;
					if (info.msg) {
						uni.showToast({
							icon: 'none',
							title: info.msg
						})
					}
					// console.log('接口错误api', api)
					resolve(info);
					// reject(info);
				}
			},
			fail: (error) => {
				if (showLoading) {
					uni.hideLoading();
				}

				console.log('接口异常api', api)
				reject(error);
			},
			complete() {
				if (showLoading) {
					uni.hideLoading();
				}
			}
		})
	})
}

const install = Vue => {
	// 获取数据字典
	Vue.prototype.getDataDic = async function (marks) {
		let that = this
		let {
			code,
			data
		} = await that.$recite_api.getDataDic(marks);
		if (code === 0) {
			return data
		}
	}

	Vue.prototype.newReturnPagesApplication = function (val = 1) {
		let that = this;
		let pages = getCurrentPages(); // 获取当前页面栈的实例
		let currPage = pages[pages.length - 1]; //当前页面
		let prevPage = pages[pages.length - 2]; //上一个页面
		if (prevPage) {
			that.applicationRewritingNavigateBack({
				delta: val
			})
		} else {
			that.applicationRewritingReLaunch({
				url: '/pages/applicationPage/home'
			})
		}
	}
}
module.exports = {
	applicationRequest: request,
	publicApplicationMethods: install
}